﻿using ASmile.Com;
using ASmile.Com.Com;
using ASmile.Com.Da;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ASmile.Extend;
using ASmile.DBModel;

namespace ASmile.Web.ControllersApi
{
    [Route("api/ASmile/[action]")]
    [ApiExplorerSettings(GroupName = "V-COM")]
    public class ComController : BaseApiController<ComBLL>
    {
        /// <summary>
        /// 保持活跃使用
        /// </summary>
        [UserAuthorize(false)]
        [HttpPost]
        public ApiResult KeepLive()
        {
            return this.RetOK($"{DateTime.Now}");
        }
        /// <summary>
        /// 刷新token
        /// </summary>
        [HttpPost]
        [LogFilter(false)]
        [UserAuthorize(true, false)]
        public ApiResult RefreshToken()
        {
            if (CurrUser.ExpiresTime - DateTime.Now > CurrUser.Expires / 3)
            {
                //如果过期时长大于token有效期的 1/2 那么返回当前token
                //token = this.HttpContext.GetToken();
                return this.RetErr("", "token不需要刷新");
            }
            else
            {
                var srcToken = HttpContext.GetToken();
                var newToken = LoginState.RefreshToken(srcToken, out LoginUser user);

                return this.RetOK(new UserLoginRet
                {
                    Token = newToken,
                    UserName = user.UserName,
                    UserId = user.UserId,
                    ExpiresTime = user.ExpiresTime
                });
            }
        }

        /// <summary>
        /// 用户登录退出,清除token
        /// </summary>
        [HttpPost]
        [UserAuthorize(true, false)]
        public ApiResult LoginOut()
        {
            var token = HttpContext.GetToken();
            LoginState.Remove(token);
            HttpContext.DelCookieToken();
            return this.RetOK("OK");
        }
        /// <summary>
        /// 登陆验证
        /// </summary>
        [UserAuthorize(false)]
        [HttpPost]
        public ApiResult<RetLoginVerify> LoginVerify(ArgLoginVerify arg)
        {
            var ret = BLLObj.LoginVerify(arg);
            if (!ret)
            {
                return ret;
            }
            var user = ret.data.UserInfo;
            user.ClientIP = HttpContext.GetRemoteIp();
            user.Expires = TimeSpan.FromMinutes(Config.THIS.TokenTimeOutApi);
            var oldToken = HttpContext.GetToken();
            if (oldToken.IsNotEmpty())
            {
                LoginState.Remove(oldToken);
            }
            
            string token = LoginState.Set(user);
            ret.data.Token = token;
            return ret;
        }
        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        [HttpPost]
        public ApiResult<Sys_User> GetUserInfo()
        {
            return BLLObj.GetUserInfo();
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        [HttpPost]
        public ApiResult SetUserInfo(Sys_User arg)
        {
            return BLLObj.SetUserInfo(arg);
        }
    }
}